{
  "properties" : { },
  "id" : "e1ee4677b8ac499193a685e4b285a5f0",
  "script" : null,
  "groupId" : "8a4d09d5d4824ed99d9cf45868c29fe1",
  "name" : "角色用户",
  "createTime" : 1714063371336,
  "updateTime" : 1711092998175,
  "lock" : null,
  "createBy" : "管理员",
  "updateBy" : "管理员",
  "path" : "set_user_role",
  "method" : "POST",
  "parameters" : [ ],
  "options" : [ {
    "name" : "permission",
    "value" : "system:jggl",
    "description" : "允许拥有该权限的访问",
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : null,
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  } ],
  "requestBody" : "{\r\n    \"unitId\": 1, \"userId\": 1001, \"roleId\": 1\r\n}",
  "headers" : [ ],
  "paths" : [ ],
  "responseBody" : null,
  "description" : null,
  "requestBodyDefinition" : null,
  "responseBodyDefinition" : null
}
================================
import com.gitee.fastmybatis.core.query.Query;
import 'com.wiwf.model.mapper.admin.dao.SysUnitUserRoleMapper' as sysUnitUserRoleMapper;

if(body.userId == 1001 && body.unitId == 1){
    exit 401, '不能切换管理员在该单位的角色'
}
if(! body.userId || ! body.unitId || ! body.roleId){
    exit 401, '数据空白'
}

var query = new Query();
query.eq("unit_id", body.unitId);
query.eq("user_id", body.userId);
query.eq("role_id", body.roleId);

if(sysUnitUserRoleMapper.getByQuery(query)){
     exit 401, '已分配'
}

try {
    sysUnitUserRoleMapper.deleteByQuery(query, null);
    return sysUnitUserRoleMapper.save(body);
} catch(e) {
    exit 500, '添加失败，可能用户已在该单位'
}